<template>
	<div>
		<van-nav-bar
		  title="注册"
		  left-arrow
		  @click-left="onClickLeft"
		  :fixed=true
		  class="away"
		/>
		<van-cell-group>
		  <van-field
		    v-model="username"
		    required
		    clearable
		    label="用户名"
		    right-icon="question-o"
		    placeholder="请输入用户名,并点击？验证"
			class="away up"
		    @click-right-icon="check"
		  />
		
		  <van-field
		    v-model="password"
		    label="密码"
		    placeholder="请输入密码"
		    required
			class="away"
		  />
		</van-cell-group>
		<van-button type="primary" size="large" class="away" @click="sure">提交</van-button>
	</div>
</template>

<script>
	import checkName from "../api/checkname.js"
	export default{
		name:"register",
		data(){
			return {
				username:"",
				password:""
			}
			
		},
		methods:{
			onClickLeft(){
				this.$router.go(-1)
			},
			check(){
				checkName({params:{username:this.username}}).then(data=>{
					this.$toast(data.data.msg)
				})
			},
			sure(){
				
				this.$store.dispatch("getUser",{username:this.username,password:this.password})
				this.$store.commit("getnames",{username:this.username})
				this.$store.commit("getpsds",{password:this.password})
				console.log(this.$store.state.regmsg)
				alert(this.$store.state.regmsg)
				if(this.$store.state.regmsg=="注册成功"){
					this.$router.push("/login")
				}else{
					alert("注册失败，该用户已被注册")
					return false
				}
				this.$router.go(0)
			}
		},
		mounted(){
			
		}
	}
</script>

<style scoped="">
	.away{
		margin-bottom: 40px;
	}
	.up{
		margin-top: 90px;
	}
</style>
